/**
 * Company
 * Copyright (C) 2004-2018 All Rights Reserved.
 */
package com.idefav.springdemo2.aop.basic.support;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @author wuzishu
 * @version $Id MethodCounter.java, v 0.1 2018-05-27 下午4:50 wuzishu Exp $$
 */
public class MethodCounter implements Serializable {
    private HashMap<String, Integer> map = new HashMap<>();

    private int allCount;

    protected void count(Method m) {
        count(m.getName());
    }

    protected void count(String methodName) {
        Integer i = map.get(methodName);
        i = (i != null) ? new Integer(i.intValue() + 1) : new Integer(1);
        map.put(methodName, i);
        ++allCount;
    }

    public int getCalls(String methodName) {
        Integer i = map.get(methodName);
        return (i != null ? i.intValue() : 0);
    }

    public int getCalls() {
        return allCount;
    }

    /**
     * A bit simplistic: just wants the same class.
     * Doesn't worry about counts.
     * @see Object#equals(Object)
     */
    @Override
    public boolean equals(Object other) {
        return (other != null && other.getClass() == this.getClass());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
